﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Data;

namespace School.Admin.Admin
{
    public partial class EditPeronnel : CheckPower
    {
        #region 传递参数
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            get
            {
                if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["id"].ToString());
            }
        }
        #endregion
        BLL.UserPic UserPic = new BLL.UserPic();
        BLL.Users Users = new BLL.Users();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.DataSource = UserPic.GetAllList();
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "ImgName";
                DropDownList1.DataBind();

                Model.UserPic c = UserPic.GetModel(1);
                Image1.ImageUrl = c.ImgURL;

                DropDownList2.DataSource = new BLL.UserType().GetAllList();
                DropDownList2.DataValueField = "ID";
                DropDownList2.DataTextField = "TypeName";
                DropDownList2.DataBind();
                DropDownList2.Items.Insert(0, new ListItem("请选择", "0"));
                if (ID > 0)
                {
                    pwd.Visible = false;
                    Model.Users u = Users.GetModel(ID);
                    if (u.UserTypeID == 2)
                    {
                        tr1.Visible = tr2.Visible = true;
                        txtShcool.Text = u.XiaoQuCount.ToString();
                        txtUploads.Text = u.UploadsCount.ToString();
                    }
                    txtLoginName.Text = u.MembeLoginName;
                    txtName.Text = u.MemberName;
                    txtPhone.Text = u.Phone;
                    txtQQ.Text = u.QQ;
                    RadioButtonList2.SelectedValue = u.Sex.ToString();
                    if (u.Birthday != null)
                    {
                        txtBirthday.Text = u.Birthday.ToString();
                    }
                    txtAddress.Text = u.Address;
                    txtDescription.Text = u.Description;
                    DropDownList2.SelectedValue = u.UserTypeID.ToString();

                    c = UserPic.GetModel(int.Parse(u.Pic));
                    Image1.ImageUrl = c.ImgURL;
                    RadioButtonList1.SelectedValue = u.State.ToString();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList2.SelectedValue == "0")
            {
                ScriptHelper.Alert("请选择用户组！");
                return;
            }
            DataSet ds = Users.GetAllList();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (txtLoginName.Text == ds.Tables[0].Rows[i]["MembeLoginName"].ToString())
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('用户名重复，请重新输入！');</script>", false);
                    return;
                }
            }

            if (ID > 0)
            {
                Model.Users u = Users.GetModel(ID);
                if (DropDownList2.SelectedValue == "2" && tr1.Visible == tr2.Visible == true)
                {
                    if (txtShcool.Text == "" || txtUploads.Text == "")
                    {
                        ScriptHelper.Alert("创建学校或上传空间大小不能为空！");
                        return;
                    }
                    else
                    {
                        u.XiaoQuCount = int.Parse(txtShcool.Text);
                        u.UploadsCount = int.Parse(txtUploads.Text);
                    }
                }

                u.MembeLoginName = txtLoginName.Text;
                u.MemberName = txtName.Text;
                u.Phone = txtPhone.Text;
                u.QQ = txtQQ.Text;
                u.Sex = int.Parse(RadioButtonList2.SelectedValue);
                if (!string.IsNullOrEmpty(txtBirthday.Text))
                {
                    u.Birthday = DateTime.Parse(DateTime.Parse(txtBirthday.Text).ToString("yyyy-MM-dd"));
                }
                u.Address = txtAddress.Text;
                u.Description = txtDescription.Text;
                u.UserTypeID = int.Parse(DropDownList2.SelectedValue);
                u.Pic = DropDownList1.SelectedValue;
                u.State = int.Parse(RadioButtonList1.SelectedValue);
                Users.Update(u);
                ScriptHelper.Alert("修改成功！");
            }
            else
            {
                if (string.IsNullOrEmpty(txtPWD.Text))
                {
                    ScriptHelper.Alert("密码不能为空");
                    return;
                }

                Model.Users u = new Model.Users();
                if (DropDownList2.SelectedValue == "2" && tr1.Visible == tr2.Visible == true)
                {
                    if (txtShcool.Text == "" || txtUploads.Text == "")
                    {
                        ScriptHelper.Alert("创建学校或上传空间大小不能为空！");
                        return;
                    }
                    else
                    {
                        u.XiaoQuCount = int.Parse(txtShcool.Text);
                        u.UploadsCount = int.Parse(txtUploads.Text);
                    }
                }
                u.MembeLoginName = txtLoginName.Text;
                u.MemberName = txtName.Text;
                u.MembePassWord = Encryption.GetMD5(txtPWD.Text);
                u.Phone = txtPhone.Text;
                u.QQ = txtQQ.Text;
                u.Sex = int.Parse(RadioButtonList2.SelectedValue);
                if (!string.IsNullOrEmpty(txtBirthday.Text))
                {
                    u.Birthday = DateTime.Parse(DateTime.Parse(txtBirthday.Text).ToString("yyyy-MM-dd"));
                }
                u.Address = txtAddress.Text;
                u.Description = txtDescription.Text;
                u.UserTypeID = int.Parse(DropDownList2.SelectedValue);
                u.Pic = DropDownList1.SelectedValue;
                u.State = int.Parse(RadioButtonList1.SelectedValue);
                u.Remark = LoginUserId.ToString();
                u.CreateTime = DateTime.Now;
                u.CreateUserID = LoginUserId;
                int s = Users.Add(u);
                if (s > 0)
                {
                    ScriptHelper.AlertRedirect("增加成功！", "Peronnel.aspx");
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Model.UserPic u = UserPic.GetModel(int.Parse(DropDownList1.SelectedValue));
            Image1.ImageUrl = u.ImgURL;
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList2.SelectedValue == "2")
            {
                tr1.Visible = tr2.Visible = true;
            }
            else
            {
                tr1.Visible = tr2.Visible = false;
            }
        }

    }
}